package managers.popupManager;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ComponentListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import managers.OptionsManager;
import utils.StackTracePrinter;
import audio.AudioManager;

public class InformationDialog extends JDialog implements Dialog {

	protected JPanel panel;
	private	JLabel iconLabel;
	private JTextPane text;
	private MutableAttributeSet nameAttributes;
	protected String eventType;

	int index;
	
	public InformationDialog(ComponentListener listener, String eventType) {
		this.eventType = eventType;
		
		setFocusable(false);
		setFocusableWindowState(false);
		setAlwaysOnTop(true);
		
		addComponentListener(listener);
		setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		
		setSize(new Dimension(326, 120));
		setTitle("Spot Messenger");
		setResizable(false);
		panel = new JPanel();
		setModal(false);
		setContentPane(panel);
		panel.setLayout(null);
		
		iconLabel = new JLabel();
		iconLabel.setBounds(12, 11, 79, 69);
		panel.add(iconLabel);
		

		text = new JTextPane();
		text.setBorder(null);
		text.setOpaque(false);
		text.setEditable(false);
		text.setFont(new Font("Tahoma", Font.PLAIN, 16));
		text.setBounds(100, 22, 212, 47);
		panel.add(text);
		
		nameAttributes = new SimpleAttributeSet();
		nameAttributes.addAttribute(StyleConstants.Bold, true);
	}
	
	
	@Override
	public int getIndex() {
		return index;
	}

	@Override
	public void setIndex(int index) {
		this.index = index;
	}

	@Override
	public void setIcon (URL path) {
		iconLabel.setIcon(new ImageIcon(path));
	}
	
	@Override
	public void setText (String	name, String message) {
		text.setText("");
		Document doc = text.getDocument();
		try {
			if (name != null)
				doc.insertString(doc.getLength(), name+"\n", nameAttributes);
			doc.insertString(doc.getLength(), message, null);
		} catch (BadLocationException e) {
			StackTracePrinter.print(e);
		}
	}
	
	
	@Override
	public void playSound () {
		String[] attributes = OptionsManager.getSharedInstance().getOptionGroup("soundPopup").getProperty(eventType+".SOUNDPATH").split(",");
		try {
			AudioManager.getSharedInstance().play(attributes[0], attributes);
		} catch (Exception e) {
			// do nothing
			StackTracePrinter.print(e);
		}
	}


	@Override
	public void setVisible() {
		this.setVisible();
	}
}


